package com.zy.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


/**
 *
 * @Project : please input
 * @Package : com.zy.utils
 * @Class : SerialCreateUtil
 * @Description :  序列号生成
 * @author : liu
 * @CreateDate : 2018-09-27 15:51:51
 * @version : V1.0.0
 * @Copyright : 2017 liu Inc. All rights reserved.
 * @Reviewed : 
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                ****    ****    **** 
 * 
 */
public class SerialCreateUtil {
	private final static String PREFIX = "DY";
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private final static int DEFAULT_LENGTH = 4;
	private final static String DATE_FORMAT = "yyyyMMdd";
	private final static String DATE_FORMATMS = "yyyyMMddHHmmss";

	/**
	 * 生成序列号
	 * 
	 *            次数
	 * @param prefix
	 *            前缀
	 *            补0的长度
	 * @return
	 */
	private static String append(String prefix){
		prefix+=new Random(3).nextInt(1000);
		return prefix;
	}
	public static String create(Long count, String prefix, int zeroLength) {
		String code = null == prefix ? PREFIX : prefix;
		code += eachLength(zeroLength, count);
		return code;
	}

	/**
	 * 生成编码
	 * 
	 * @param count
	 * @return
	 */
	public static String createByTime(Long count,String prefix) {
		String code = null == prefix ? PREFIX : prefix;
			 code += generateByTime();
		code += eachLength(DEFAULT_LENGTH, count);
		return code;
	}

	public static String createOutDetail(Long count) {
		return generateByTime() + eachLength(DEFAULT_LENGTH, count);
	}

	public static String eachLength(Integer length, Long count) {
		String code = "";
		Long partsOfCode = count + 1;
		for (int i = 0; i < length - (partsOfCode + "").length(); i++) {
			code += "0";
		}
		return code + partsOfCode;
	}

	public static String generateByTime() {
		String str = new SimpleDateFormat(DATE_FORMAT).format(Calendar.getInstance().getTime());
		return str;

	}

	public static String generateByTimeYMS() {
		String str = new SimpleDateFormat(DATE_FORMATMS).format(Calendar.getInstance().getTime());
		return str;

	}
	public static String generateByTimeYMS(Date date) {
		String str = new SimpleDateFormat(DATE_FORMAT).format(date.getTime());
		return str;

	}


	/**
	 * 生成序列号
	 * 
	 * @param count
	 *            次数
	 * @param prefix
	 *            后綴
	 * @return
	 */
	public static String createSerial(Long count, String prefix) {
		return create(count, prefix, DEFAULT_LENGTH);

	}

	public static String createRandom() {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < 8; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
		}
		return sb.toString();
	}
	private static String getRandom(int strLength) {

		Random rm = new Random();

		// 获得随机数
		double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

		// 将获得的获得随机数转化为字符串
		String fixLenthString = String.valueOf(pross);

		// 返回固定的长度的随机数
		return fixLenthString.substring(1, strLength + 1);
	}
	public static String getRandom(String perfix) {
		perfix+=getRandom(3);
	   return perfix;
	}
	public static void main(String[] args) {
		 System.out.print(eachLength(4,4L));
	}
}
